import {
  apiStoremanagerActivityList,
  apiAuditActivityList,
  apiAuditStoreList,
  apiAuditCreateActivity,
  apiOrderDetailByOrderNo,
  apiActivityImageList,
  apiMaterial,
  apiActivityDetail,
  apiMakeOrder,
  apiStorePosition,
  apiActivityOrderList,
  apiActivityOrderDetail,
  apiAuditOrder,
  apiDeleteActivity,
  apiOpenStore,
  apiOrderStatusList,
  apiAuditAirOrder
} from '../other/api'

import { regeneratorRuntime, dayjs } from '../other/bundle'

const initState = {
  auditActivityList: {
    actionStatus: 'INIT',
    data: {}
  },
  auditLatestActivityList: {
    actionStatus: 'INIT',
    data: {}
  },
  auditHistoryActivityList: {
    actionStatus: 'INIT',
    data: {}
  },
  auditStoreList: {
    actionStatus: 'INIT',
    data: {}
  },
  auditCreateActivity: {
    actionStatus: 'INIT',
    data: {}
  },
  activityDetail: {
    actionStatus: 'INIT',
    data: {
    }
  },
  orderDetail: {
    actionStatus: 'INIT',
    data: {
      ads_info: []
    }
  },
  activityImageList: {
    actionStatus: 'INIT',
    data: {}
  },
  materialList: {
    actionStatus: 'INIT',
    data: {}
  },
  storePosition: {
    actionStatus: 'INIT',
    data: {}
  },
  mosStep1: {},
  mosStep2: {},
  customAdsPositionData: {},
  activityOrderList: {
    actionStatus: 'INIT',
    data: {}
  },
  activityOrderDetail: {
    actionStatus: 'INIT',
    data: {}
  },
  orderStatusList: {
    actionStatus: 'INIT',
    data: {}
  },
}

// 审核员首页活动列表
export const getAuditActivityList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditActivityList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => {
          i.uiActivityStartTime = dayjs(i.activity_start_time * 1000).format('YYYY-MM-DD')
          i.uiActivityEndTime = dayjs(i.activity_end_time * 1000).format('YYYY-MM-DD')
          i.uiUpStartTime = dayjs(i.up_start_time * 1000).format('YYYY-MM-DD')
          i.uiUpEndTime = dayjs(i.up_end_time * 1000).format('YYYY-MM-DD')
          i.uiAddTime = dayjs(i.add_time * 1000).format('YYYY-MM-DD')
          i.uiOrderEndTime = dayjs(i.order_end_time * 1000).format('YYYY-MM-DD')
          i.uiStoreNum = i.store_ids.split(',').filter(i => i).length
          i.uiImageNum = i.img_ids.split(',').filter(i => i).length
          i.uiUpdateTime = dayjs(i.update_time * 1000).format('YYYY-MM-DD')
          i.uiStorePlacedNum = i.store_placed_num
          i.uiStatus = ((status) => {
            switch (status) {
              case 0:
                return '未完成'
              case 1:
                return '已完成'
              default:
                return '未完成'
            }
          })(i.status)
        })
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_AUDIT_ACTIVITY_LIST' })
    return data;
  }
}
// 审核员最新活动列表
export const getAuditLatestActivityList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditActivityList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => {
          i.uiActivityStartTime = dayjs(i.activity_start_time * 1000).format('YYYY-MM-DD')
          i.uiActivityEndTime = dayjs(i.activity_end_time * 1000).format('YYYY-MM-DD')
          i.uiUpStartTime = dayjs(i.up_start_time * 1000).format('YYYY-MM-DD')
          i.uiUpEndTime = dayjs(i.up_end_time * 1000).format('YYYY-MM-DD')
          i.uiAddTime = dayjs(i.add_time * 1000).format('YYYY-MM-DD')
          i.uiOrderEndTime = dayjs(i.order_end_time * 1000).format('YYYY-MM-DD')
          i.uiStoreNum = i.store_ids.split(',').filter(i => i).length
          i.uiImageNum = i.img_ids.split(',').filter(i => i).length
          i.uiUpdateTime = dayjs(i.update_time * 1000).format('YYYY-MM-DD')
          i.uiStorePlacedNum = i.store_placed_num
          i.uiStatus = ((status) => {
            switch (status) {
              case 0:
                return '未完成'
              case 1:
                return '已完成'
              default:
                return '未完成'
            }
          })(i.status)
        })
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_AUDIT_LATEST_ACTIVITY_LIST' })
    return data;
  }
}
// 审核员历史活动列表
export const getAuditHistoryActivityList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditActivityList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => {
          i.uiActivityStartTime = dayjs(i.activity_start_time * 1000).format('YYYY-MM-DD')
          i.uiActivityEndTime = dayjs(i.activity_end_time * 1000).format('YYYY-MM-DD')
          i.uiUpStartTime = dayjs(i.up_start_time * 1000).format('YYYY-MM-DD')
          i.uiUpEndTime = dayjs(i.up_end_time * 1000).format('YYYY-MM-DD')
          i.uiAddTime = dayjs(i.add_time * 1000).format('YYYY-MM-DD')
          i.uiOrderEndTime = dayjs(i.order_end_time * 1000).format('YYYY-MM-DD')
          i.uiStoreNum = i.store_ids.split(',').filter(i => i).length
          i.uiImageNum = i.img_ids.split(',').filter(i => i).length
          i.uiUpdateTime = dayjs(i.update_time * 1000).format('YYYY-MM-DD')
          i.uiStorePlacedNum = i.store_placed_num
          i.uiStatus = ((status) => {
            switch (status) {
              case 0:
                return '未完成'
              case 1:
                return '已完成'
              default:
                return '未完成'
            }
          })(i.status)
        })
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_AUDIT_HISTORY_ACTIVITY_LIST' })
    return data;
  }
}

export const getStoremanagerActivityList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiStoremanagerActivityList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => {
          i.uiActivityStartTime = dayjs(i.activity_start_time * 1000).format('YYYY-MM-DD')
          i.uiActivityEndTime = dayjs(i.activity_end_time * 1000).format('YYYY-MM-DD')
          i.uiUpStartTime = dayjs(i.up_start_time * 1000).format('YYYY-MM-DD')
          i.uiUpEndTime = dayjs(i.up_end_time * 1000).format('YYYY-MM-DD')
          i.uiAddTime = dayjs(i.add_time * 1000).format('YYYY-MM-DD')
          i.uiOrderEndTime = dayjs(i.order_end_time * 1000).format('YYYY-MM-DD')
          i.uiStoreNum = i.store_ids.split(',').filter(i => i).length
          i.uiImageNum = i.img_ids.split(',').filter(i => i).length
          i.uiUpdateTime = dayjs(i.update_time * 1000).format('YYYY-MM-DD')
          i.uiStorePlacedNum = i.store_placed_num
          i.uiStatus = ((status) => {
            switch (status) {
              case 0:
                return '未完成'
              case 1:
                return '已完成'
              default:
                return '未完成'
            }
          })(i.status)
        })
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_AUDIT_ACTIVITY_LIST' })
    return data;
  }
}

// 高空审核
export const postAuditAirOrder = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditAirOrder(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_POST_AUDIT_AIR_ORDER_RES' })
    return data;
  }
}

export const getAuditStoreList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditStoreList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => {
          i.name = i.store_name
          i.checked = true
          i.show = true
        })
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_AUDIT_STORE_LIST' })
    return data;
  }
}

export const createActivity = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditCreateActivity(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_CREATE_ACTIVITY_RES' })
    return data;
  }
}

export const getOrderDetailByOrderNo = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiOrderDetailByOrderNo(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {

        apiRes.data.up_time = Number(apiRes.data.up_time)
        apiRes.data.down_time = Number(apiRes.data.down_time)
        apiRes.data.uiActivityUpTime = apiRes.data.up_time ? dayjs(apiRes.data.up_time * 1000).format('YYYY-MM-DD') : ''
        apiRes.data.uiActivityDownTime = apiRes.data.down_time ? dayjs(apiRes.data.down_time * 1000).format('YYYY-MM-DD') : ''

        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ORDER_DETAIL' })
    return data;
  }
}

export const getMaterial = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiMaterial(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_MATERIAL' })
    return data;
  }
}

export const getActivityImageList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiActivityImageList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ACTIVITY_IMAGE' })
    return data;
  }
}

export const getActivityDetail = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiActivityDetail(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiUpEndTime = dayjs(apiRes.data.up_end_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiUpStartTime = dayjs(apiRes.data.up_start_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ACTIVITY_DETAIL' })
    return data;
  }
}

export const makeOrder = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiMakeOrder(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_MAKE_ORDER_RESULT' })
    return data;
  }
}

export const getStorePosition = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiStorePosition(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_STORE_POSITION' })
    return data;
  }
}

export const getActivityOrderList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiActivityOrderList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ACTIVITY_ORDER_LIST' })
    return data;
  }
}

export const getActivityOrderDetail = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiActivityOrderDetail(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')

        apiRes.data.uiActivityUpTime = dayjs(apiRes.data.up_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityDownTime = dayjs(apiRes.data.down_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ACTIVITY_ORDER_DETAIL' })
    return data;
  }
}

export const postAuditOrder = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditOrder(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.uiActivityStartTime = dayjs(apiRes.data.activity_start_time * 1000).format('YYYY-MM-DD')
        apiRes.data.uiActivityEndTime = dayjs(apiRes.data.activity_end_time * 1000).format('YYYY-MM-DD')
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_POST_AUDIT_ORDER_RES' })
    return data;
  }
}

export const deleteActivity = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiDeleteActivity(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_DELETE_ACTIVITY_RESULT' })
    return data;
  }
}

export const openStore = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiOpenStore(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_OPEN_RES' })
    return data;
  }
}

export const getOrderStatusList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiOrderStatusList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        apiRes.data.map(i => {
          if (i.add_time) {
            i.uiDate = dayjs(i.add_time * 1000).format('YYYY-MM-DD')
          }
        })
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ORDER_STATUS_LIST' })
    return data;
  }
}

export const ayncMakeOrderForm = data => ({
  type: 'SYNC_MAKE_ORDER_FORM',
  data
})

export const adsPositionSelect = data => ({
  type: 'ADS_POSITION_SELECT',
  data
})

export const asyncAdsPostionSettingData = data => ({
  type: 'ASYNC_ADS_POSITION_SETTING_DATA',
  data
})
export const asyncAdsPostionSettingSubmit = data => ({
  type: 'ASYNC_ADS_POSITION_SETTING_SUBMIT',
  data
})
export const asyncAdsBlockSettingData = data => ({
  type: 'ASYNC_ADS_BLOCK_SETTING_DATA',
  data
})
export const asyncAdsBlockSettingSubmit = data => ({
  type: 'ASYNC_ADS_BLOCK_SETTING_SUBMIT',
  data
})

export const initCustomAdsPositionData = data => ({
  type: 'INIT_CUSTOM_ADS_POSITION_DATA',
  data
})
export const customAdsPositionSubmit = data => ({
  type: 'CUSTOM_ADS_POSITION_SUBMIT',
  data
})
export const asyncDetailToPosition = data => ({
  type: 'ASYNC_DETAIL_TO_POSITION',
  data
})
export const asyncPositionNum = data => ({
  type: 'ASYNC_POSITION_NUM',
  data
})

export const reducer = (state = initState, action) => {
  switch (action.type) {
    // 订单状态列表
    case 'SET_ORDER_STATUS_LIST': {
      return {
        ...state,
        orderStatusList: action.data
      }
    }
    // 广告位物料数量
    case 'ASYNC_POSITION_NUM': {
      const { num, index } = action.data
      state.storePosition.data[index].tpl.num = num
      return state
    }
    // 设置审核结果
    // 活动订单列表
    case 'SET_ACTIVITY_ORDER_LIST': {
      return {
        ...state,
        activityOrderList: action.data
      }
    }
    case 'SET_ACTIVITY_ORDER_DETAIL': {
      return {
        ...state,
        activityOrderDetail: action.data
      }
    }
    // 活动订单详情
    // 下单广告位List
    case 'SET_STORE_POSITION': {
      const storePosition = action.data
      storePosition.data = storePosition.data.map((item, index) => {
        item.index = index // 补充广告位索引
        item.num = 1 // 补充广告位物料数量 默认 1份
        item.ads_type = item.ads_type || 1 // 补充广告类型 默认系统
        item.tpl = JSON.parse(JSON.stringify(item))

        item.img = item.filepath
        item.image_url = item.filepath

        item.selected = item.tpl.detail.reduce((prev, next) => Boolean(prev && (next.image_id && next.materiel_id)), true) // 默认不选中
        return item
      })
      return {
        ...state,
        storePosition
      }
    }
    // 把广告位详情merge到修改结构中
    case 'ASYNC_DETAIL_TO_POSITION': {
      const { orderDetail } = state
      const originData = JSON.parse(JSON.stringify(orderDetail.data.ads_info))
      const targetData = originData.map((position, j) => {
        position.index = j
        position.positionIndex = j


        // 系统广告位
        if (position.ads_type == 1) {

          position.img = position.filepath
          position.image_id = position.activity_image_id
          position.image_url = position.filepath
          position.image_name = position.activity_image_name || '-'

          const detail = position.detail.map((block, k) => {
            return {
              ...block,
              // ad_name: block.ads_name,
              block_name: block.ads_name,
              type: block.ads_type,
              image_id: block.activity_image_id,
              image_url: block.activity_image_url,
              image_name: block.activity_image_name,
              positionIndex: j
            }
          })
          position.tpl = {
            ...position,
            positionIndex: j,
            detail: detail
          }
          position.selected = position.tpl.detail.reduce((prev, next) => {
            return Boolean(prev && (next.image_id && next.materiel_id))
          }, true)
          return position
        }
        // 自定义广告位
        if (position.ads_type == 2) {
          position.ads_id = position.id
          position.img = position.activity_image_url
          position.image_id = position.activity_image_id
          position.image_url = position.activity_image_url
          position.image_name = position.activity_image_name
          position.tpl = {
            ...position,
            positionIndex: j,
          }
          position.selected = position.image_id && position.materiel_id && position.ads_name && position.width && position.height
          return position
        }
      })
      console.log('--res', originData, targetData)
      return {
        ...state,
        storePosition: {
          ...state.storePosition,
          data: targetData
        }
      }
    }
    case 'INIT_CUSTOM_ADS_POSITION_DATA': {
      state.customAdsPositionData = action.data
      return state
    }
    case 'CUSTOM_ADS_POSITION_SUBMIT': {
      const { index } = action.data
      const originData = state.storePosition
      let targetData = JSON.parse(JSON.stringify(originData))

      action.data.img = action.data.img || action.data.image_url
      targetData.data[index] = { ...targetData.data[index], ...action.data }
      state.storePosition = targetData

      // action.data.img = action.data.img || action.data.image_url
      // state.storePosition.data[index] = { ...state.storePosition.data[index], ...action.data }
      // state.orderDetail.data.ads_info[index] = action.data
      // state.orderDetail.data.ads_info.push(action.data)
      return state
    }
    case 'SET_ORDER_DETAIL': {
      return {
        ...state,
        orderDetail: action.data
      }
    }
    case 'ASYNC_ADS_BLOCK_SETTING_DATA': {
      const { mosStep1 } = state
      const { index } = action.data
      const mosStep2 = mosStep1.detail[index]
      mosStep2.index = index
      return {
        ...state,
        mosStep2
      }
    }
    case 'ASYNC_ADS_BLOCK_SETTING_SUBMIT': {
      console.log(action.data)
      const { mosStep1, storePosition } = state
      const { positionIndex, index } = mosStep1
      storePosition.data[positionIndex].tpl.detail[index] = mosStep1
      storePosition.data[positionIndex].selected = storePosition.data[positionIndex].tpl.detail.reduce((prev, next) =>
        Boolean(prev && (next.image_id && next.materiel_id)), true) // 默认不选中
      state.storePosition = JSON.parse(JSON.stringify(storePosition))
      return state
    }
    case 'ASYNC_ADS_POSITION_SETTING_DATA': {
      return {
        ...state,
        mosStep1: action.data
      }
    }
    case 'ASYNC_ADS_POSITION_SETTING_SUBMIT': {
      const { num } = action.data
      const { orderDetail, mosStep1 } = state
      const { index } = mosStep1
      mosStep1.num = num
      orderDetail.data.ads_info[index] = mosStep1
      return {
        ...state,
        orderDetail,
        mosStep1: {}
      }
    }
    case 'ADS_POSITION_SELECT': {
      const { index, selected } = action.data
      state.storePosition.data[index].selected = selected
      // state.orderDetail.data.ads_info[index].selected = selected
      return JSON.parse(JSON.stringify(state))
    }
    case 'SYNC_MAKE_ORDER_FORM': {
      return {
        ...state,
        orderDetail: {
          ...state.orderDetail,
          data: {
            ...state.orderDetail.data,
            ...action.data
          }
        }
      }
    }
    case 'SET_ACTIVITY_DETAIL': {
      return {
        ...state,
        activityDetail: action.data
      }
    }
    case 'SET_ACTIVITY_IMAGE': {
      return {
        ...state,
        activityImageList: action.data
      }
    }
    case 'SET_MATERIAL': {
      return {
        ...state,
        materialList: action.data
      }
    }
    // 审核员首页活动列表
    case 'SET_AUDIT_ACTIVITY_LIST':
      return {
        ...state,
        auditActivityList: action.data
      }
    // 审核员最新活动列表
    case 'SET_AUDIT_LATEST_ACTIVITY_LIST':
      return {
        ...state,
        auditLatestActivityList: action.data
      }
    // 审核员历史活动列表
    case 'SET_AUDIT_HISTORY_ACTIVITY_LIST':
      return {
        ...state,
        auditHistoryActivityList: action.data
      }
    case 'SET_AUDIT_STORE_LIST':
      return {
        ...state,
        auditStoreList: action.data
      }
      break;
    case 'SET_CREATE_ACTIVITY_RES':
      return {
        ...state,
        auditCreateActivity: action.data
      }
      break;
    default:
      return state
      break;
  }
}

export default {
  reducer,
  getAuditStoreList,
  createActivity,
  getAuditActivityList,
}

